package cn.chencaiju.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.druid.util.StringUtils;

import cn.chencaiju.domain.Category;
import cn.chencaiju.domain.Post;
import cn.chencaiju.service.CategoryService;
import cn.chencaiju.service.PostService;

@Controller
@RequestMapping("/post/")
public class PostController {
	@Autowired
	PostService postService;
	@Autowired
	CategoryService cateService;
	
	@RequestMapping(path="/load")
	public ModelAndView load(Model m) {
		System.out.println("aaa");
		List<Post> list = postService.loadPost();
		m.addAttribute("posts", list);
		ModelAndView mv = new ModelAndView();	// 跳到哪个页面（view），页面上需要显示什么数据（model）
		mv.setViewName("posts");
		m.addAttribute("categories", cateService.findAll());
		return mv;
	}
	@RequestMapping("/updateForm")
	public String updateForm() {
		
		return "posts";
	}
	@RequestMapping("/insert")
	public String insert(int id) {
		Post p=new Post();
		p.setId(id);
		p.setTitle("title"+id);
		p.setContent("content"+id);
		postService.insert(p);
		return "success";
	}
	@RequestMapping("/form")
	public String goForm(@RequestParam(name = "id", defaultValue = "-1")int id,
			Model m) {
		System.out.println("form...\tid="+id);
		if(id!=-1) {
			m.addAttribute("id", id);
			m.addAttribute("action", "edit.action");
		}else {
			m.addAttribute("action", "addPost.action");
		}
		m.addAttribute("categories", cateService.findAll());
		return "post_form";
	}
	@RequestMapping("/edit")
	public String editPost(Post p, Model model, @RequestParam("uploadFile") MultipartFile file) {
		System.out.println("edit...");
		postService.update(p);
		// 把请求直接转发到另外一个action
		
		model.addAttribute("posts", p);
		// 文件名字：
		if(file!=null&&!file.isEmpty()) {
			String fileName = file.getOriginalFilename();
			System.out.println("上传的文件名：" + fileName);
		}
		return "forward:/post/load.action";
	}
	@RequestMapping("/delete")
	public String delete(int id) {
		System.out.println("delete"+id);
		postService.delete(id);
		return "forward:/post/load";
	}
	@RequestMapping("/delBatch")
	public String delBatch(@RequestParam("ids") String[] ids) {
		postService.delBatch(ids);
		return "forward:/post/load";
	}
	@RequestMapping(path="/search",method=RequestMethod.POST)
	public String search(Post p,Model model) {
		System.out.println(p);
		if(!StringUtils.isEmpty(p.getTitle())) {
			model.addAttribute("title", p.getTitle());
		}
		if(p.getCategory()!=null&&p.getCategory().getId()!=null) {
			model.addAttribute("category", p.getCategory());
		}
		if(p.getCreated()!=null) {
			model.addAttribute("created", p.getCreated());
		}
		List<Category> categories = cateService.findAll();
		List<Post> list =postService.search(p);
		model.addAttribute("posts", list);
		model.addAttribute("categories", categories);
		return "posts";	
	}
	@RequestMapping("addPost")
	public String addPost(Post p,Model model) {
		System.out.println("addPost...");
		System.out.println(p);
		postService.insert(p);
		return "forward:/post/load";
	}
	
}
